//----
// PART IV - Shares by Entity Classification


	/* This file uses Schedule K-1 data broken down by
	entity classification to describe the composition
	of partners, broken down by net income, compared
	to previous research, and also by counts. It also
	produces Figures 5, 12, and 13 */

// ----------------
// Calculate entity shares by net income
		
	use ${data_dir}/income_table.dta, clear
	
	keep if year>2010
	
	// Refine categories
		
		/* "Y" represent limited liability entites that are not
		taxed as partnerships. These are best treated as C corporaitons
		when organizing entities into bins comparable to Cooper et al." */
		
		replace class="C" if class=="Y" 
		
		// unknown partners
		
		replace class="X" if class==""
		
		// foreign partners that are not partnerships
		
		replace class="F" if dest!="US" & dest!="" & class!="P" 
		
	// Collapse	
		
		gcollapse (sum) net_income, by(class_entity)
		
		egen sumnet = sum(net_income)
		g newshare = net_income/sumnet*100
		
	// Get shares without partnerships
	
		sum newshare if class_entity=="P"
		g pshare = r(mean)
		g tshare = 100-pshare
		g nopshp = newshare/tshare*100
		replace nopshp = . if class_entity=="P"	
		
	
	// Input Cooper et al. shares	
			
		g oldshare = .
		replace oldshare=31.5 if class_entity=="H"
		replace oldshare=26.3 if class_entity=="P"
		replace oldshare=7.7 if class_entity=="C"
		replace oldshare=3.4 if class_entity=="S"
		replace oldshare=6.8 if class_entity=="F"
		replace oldshare=5.2 if class_entity=="Z"
		replace oldshare=5.3 if class_entity=="T"
		replace oldshare=4.1 if class_entity=="N"
		replace oldshare=9.6 if class_entity=="X"
	
	// Set order
	
		g ord = 0
		replace ord=1 if class_entity=="H"
		replace ord=2 if class_entity=="P"
		replace ord=3 if class_entity=="C"
		replace ord=4 if class_entity=="S"
		replace ord=5 if class_entity=="F"
		replace ord=6 if class_entity=="Z"
		replace ord=7 if class_entity=="T"
		replace ord=8 if class_entity=="N"
		replace ord=9 if class_entity=="D"
		replace ord=10 if class_entity=="X"
		
		sort ord
		
		g clabel = ""
		replace clabel = "C-Corporation" if class_entity=="C"
		replace clabel = "S-Corporation" if class_entity=="S"
		replace clabel = "Individual" if class_entity=="H"
		replace clabel = "Partnership" if class_entity=="P"
		replace clabel = "Trust & Estates" if class_entity=="T"
		replace clabel = "Tax Exempt" if class_entity=="N"
		replace clabel = "Entity" if class_entity=="Z"
		replace clabel = "Foreign" if class_entity=="F"
		replace clabel = "Custodial" if class_entity=="D"
		replace clabel = "Unknown" if class_entity=="X"
		
		
		
	// clean
	
		keep ord clabel oldshare newshare nopshp 
		order ord clabel oldshare newshare nopshp
		
		replace newshare = round(newshare,.1)
		replace nopshp = round(nopshp,.1)
		
	// Figure 5
		
		preserve 
		 
		keep ord clabel nopshp newshare
		export delimited using ${out_dir}/fig5_tab.csv, replace
			
		labmask ord, values(clabel)
		
		// Grayscale
		
		graph bar newshare nopshp, over(ord) ///
			bar(1, color(gray*1.75)) ///
			bar(2, color(gray*.6)) ///
			horiz blabel(total, format(%5.1f)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("", height(6)) ///
			ylabel(0(5)40,labsize(small)) ///
			leg(cols(2) pos(6) order(1 "Share Including Partnerhsips" 2 "Share Excluding Partnerships") region(lcolor(white)))
			graph export ${fig_dir}/fig_ent_class.png, replace width(1000)
			
		// Color
			
		graph bar newshare nopshp, over(ord) ///
			bar(1, color(navy)) ///
			bar(2, color(maroon)) ///
			horiz blabel(total, format(%5.1f)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("", height(6)) ///
			ylabel(0(5)40,labsize(small)) ///
			leg(cols(2) pos(6) order(1 "Share Including Partnerhsips" 2 "Share Excluding Partnerships") region(lcolor(white)))
			graph export ${fig_dir}/fig_ent_class_color.png, replace width(1000)
			
	
			
			
		restore	
		
	// Figure 12
			
		keep ord clabel oldshare newshare
		export delimited using ${out_dir}/fig12_tab.csv, replace
				
		labmask ord, values(clabel)
		
		graph bar oldshare newshare, over(ord) ///
			bar(1, color(gray*1.75)) ///
			bar(2, color(gray*.6)) ///
			horiz blabel(total, format(%5.1f)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("", height(6)) ///
			ylabel(0(5)40,labsize(small)) ///
			leg(cols(2) pos(6) order(1 "Cooper et al." 2 "This Project") region(lcolor(white)))
			graph export ${fig_dir}/fig_cooper_class.png, replace width(1000)
		
			
		graph bar oldshare newshare, over(ord) ///
			bar(1, color(navy)) ///
			bar(2, color(maroon)) ///
			horiz blabel(total, format(%5.1f)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("", height(6)) ///
			ylabel(0(5)40,labsize(small)) ///
			leg(cols(2) pos(6) order(1 "Cooper et al." 2 "This Project") region(lcolor(white)))
			graph export ${fig_dir}/fig_cooper_class_color.png, replace width(1000)
								
	

// ----------------
// Calculate entity shares by count
		
	use ${data_dir}/counts_table.dta, clear
	
	keep if year>2010
	
	// Refine categories
		
		// unknown partners
		
		replace class="X" if class==""
		
		// foreign partners that are not partnerships
		
		replace class="F" if dest!="US" & dest!="" & class!="P" 
		
	// Count K1s
	
		g byte obs = 1
		
		gcollapse (sum) obs, by(class_entity)
		
	// Set order
	
		g ord = 0
		replace ord=1 if class_entity=="H"
		replace ord=2 if class_entity=="P"
		replace ord=3 if class_entity=="C"
		replace ord=4 if class_entity=="S"
		replace ord=5 if class_entity=="F"
		replace ord=6 if class_entity=="Z"
		replace ord=7 if class_entity=="T"
		replace ord=8 if class_entity=="N"
		replace ord=9 if class_entity=="D"
		replace ord=10 if class_entity=="X"	
	
		sort ord
		
		g clabel = ""
		replace clabel = "Individual" if ord==1
		replace clabel = "Partnership" if ord==2
		replace clabel = "C-Corporation" if ord==3
		replace clabel = "S-Corporation" if ord==4
		replace clabel = "Foreign" if ord==5
		replace clabel = "Entity" if ord==6
		replace clabel = "Trust & Estates" if ord==7
		replace clabel = "Tax Exempt" if ord==8
		replace clabel = "Custodial" if ord==9
		replace clabel = "Unknown" if ord==10
		
		drop if clabel==""
		
		// Calculate shares
		
		sum obs
		g tot = r(sum)
		g share = obs/tot*100
		
	//----
	// Figure 13

		// Clean
		
		keep ord clabel share
		
		labmask ord, values(clabel)
		
		graph bar share, over(ord) bar(1,color(gray*1.5)) horiz blabel(total, format(%5.1f)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("", height(6)) ///
			ylabel(0(10)80,labsize(small)) ///
			leg(off)
			graph export ${fig_dir}/fig_entshare_class.png, replace width(1000)
		
		graph bar share, over(ord) bar(1,color(navy)) horiz blabel(total, format(%5.1f)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("", height(6)) ///
			ylabel(0(10)80,labsize(small)) ///
			leg(off)
			graph export ${fig_dir}/fig_entshare_class_color.png, replace width(1000)

	// Export
			
		replace share= round(share,.1)
	
		export delimited using ${out_dir}/fig13_tab.csv, replace
			
